//
//  TBRRentPersonMessageC.m
//  TBText
//
//  Created by yy on 2018/4/30.
//  Copyright © 2018年 yy. All rights reserved.
//

#import "TBRRentPersonMessageC.h"

#import "TBRRentPersonMessageCell.h"
#import "TBRHistoryRentPersonEnumCell.h"

#import <YYModel.h>

#import "TBRAddressBookListC.h"
@interface TBRRentPersonMessageC ()<UITableViewDelegate, UITableViewDataSource,TBRRentPersonMessageCellDelegate>
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *historyArray;//历史数据
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)TBRRentPersonInfoModel *personInfoModel;
@end

@implementation TBRRentPersonMessageC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
    [self loadData];
}

- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRRentPersonMessageCell" bundle:nil] forCellReuseIdentifier:@"TBRRentPersonMessageCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRHistoryRentPersonEnumCell" bundle:nil] forCellReuseIdentifier:@"TBRHistoryRentPersonEnumCellId"];
}

- (void)setupNavItem {
    self.titleText = @"乘车人信息";
    [self ISG_NavigationBarRightBarWithTitle:@"确认" andAction:@selector(sureBtnClick)];
}

- (void)sureBtnClick {
    __weak typeof(self) weakSelf = self;
    [self showFinishBtnClickAlertShowMessage:nil block:^{
        if (weakSelf.rentPersonMessageBlock) {
            weakSelf.rentPersonMessageBlock(weakSelf.personInfoModel);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
}
#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? 1 : self.historyArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {

    NSString *iden = @"TBRRentPersonMessageCellId";
    if (indexPath.section == 0) {
        iden = @"TBRRentPersonMessageCellId";
    }else {
        iden = @"TBRHistoryRentPersonEnumCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
//#import "TBRRentPersonMessageC.h"
//#import "TBRHistoryRentPersonCell.h"
//#import "TBRHistoryRentPersonEnumCell.h"
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        TBRRentPersonMessageCell *cellPersonM = (TBRRentPersonMessageCell *)cell;
        cellPersonM.lbe_historyTitle.hidden = (_historyArray.count == 0 || !_historyArray);
        cellPersonM.personInfoModel = self.personInfoModel;
        cellPersonM.delegate = self;
    }else {
        TBRRentPersonInfoModel *personInfoModel = self.historyArray[indexPath.row];
        TBRHistoryRentPersonEnumCell *historyRentPersonEnumCell = (TBRHistoryRentPersonEnumCell *)cell;
        historyRentPersonEnumCell.rentPersonInfoModel = personInfoModel;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {return 270;}
    return 47;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        TBRRentPersonInfoModel *personInfoModel = self.historyArray[indexPath.row];
        self.personInfoModel.personName = personInfoModel.personName;
        self.personInfoModel.personNameCall = personInfoModel.personNameCall;
        self.personInfoModel.phoneNum = personInfoModel.phoneNum;
        [self.tableView reloadData];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.section == 1) {
        return TRUE;
    }
    return false;
    
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{
    if (indexPath.section == 1) {
        return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
    
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        if (editingStyle ==UITableViewCellEditingStyleDelete) {//如果编辑样式为删除样式
            if (indexPath.row<[self.historyArray count]) {
                [self.historyArray removeObjectAtIndex:indexPath.row];//移除数据源的数据
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];//移除tableView中的数据
                if (self.historyArray.count == 0) {
                    [self.tableView reloadData];
                }
            }
            
        }
    }
}


- (void)personMessageCellAddressBookBtnClick {
    TBRAddressBookListC *addressBookListC = [[TBRAddressBookListC alloc] init];
    addressBookListC.personInfoModel = self.personInfoModel;
    __weak typeof(self) weakSelf = self;
    //点击确定后数据取回来重新刷新界面
    addressBookListC.addressBookListBlock = ^{
        [weakSelf.tableView reloadData];
    };
    [self.navigationController pushViewController:addressBookListC animated:YES];
}


// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    self.dataArray = [NSMutableArray array];
    NSArray *tmpArray = @[
                              @{@"personName":@"丁晓龙",
                                @"personNameCall":@"丁先生",
                                @"phoneNum":@"13373633086",
                              },
                              @{@"personName":@"移动客服",
                                @"personNameCall":@"移动客服",
                                @"phoneNum":@"10086",
                                },
                              @{@"personName":@"电信客服",
                                @"personNameCall":@"丁先生",
                                @"phoneNum":@"1000",
                                }
                          ];
    self.historyArray = [NSArray yy_modelArrayWithClass:[TBRRentPersonInfoModel class] json:tmpArray].mutableCopy;
    [self.tableView reloadData];
    
    _personInfoModel = [[TBRRentPersonInfoModel alloc] init];
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    
}
@end
